1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package com.google.common.collect.testing.testers;
16
17 import static com.google.common.collect.testing.features.CollectionSize.ONE;
18 import static com.google.common.collect.testing.features.CollectionSize.ZERO;
19 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
20 import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
21
22 import com.google.common.annotations.GwtCompatible;
23 import com.google.common.collect.testing.AbstractMapTester;
24 import com.google.common.collect.testing.features.CollectionSize;
25 import com.google.common.collect.testing.features.MapFeature;
26
27 import java.util.LinkedHashMap;
28 import java.util.Map;
29 import java.util.Map.Entry;
30 import java.util.Set;
31
32
33
34
35
36
37
38
39 @GwtCompatible
40 public class MapToStringTester<K, V> extends AbstractMapTester<K, V> {
41 public void testToString_minimal() {
42 assertNotNull("toString() should not return null", getMap().toString());
43 }
44
45 @CollectionSize.Require(ZERO)
46 public void testToString_size0() {
47 assertEquals("emptyMap.toString should return {}", "{}", getMap().toString());
48 }
49
50 @CollectionSize.Require(ONE)
51 public void testToString_size1() {
52 assertEquals(
53 "size1Map.toString should return {entry}", "{" + samples.e0 + "}", getMap().toString());
54 }
55
56 @CollectionSize.Require(absent = ZERO)
57 @MapFeature.Require(ALLOWS_NULL_KEYS)
58 public void testToStringWithNullKey() {
59 initMapWithNullKey();
60 testToString_formatting();
61 }
62
63 @CollectionSize.Require(absent = ZERO)
64 @MapFeature.Require(ALLOWS_NULL_VALUES)
65 public void testToStringWithNullValue() {
66 initMapWithNullValue();
67 testToString_formatting();
68 }
69
70 public void testToString_formatting() {
71 assertEquals(
72 "map.toString() incorrect", expectedToString(getMap().entrySet()), getMap().toString());
73 }
74
75 private String expectedToString(Set<Entry<K, V>> entries) {
76 Map<K, V> reference = new LinkedHashMap<K, V>();
77 for (Map.Entry<K, V> entry : entries) {
78 reference.put(entry.getKey(), entry.getValue());
79 }
80 return reference.toString();
81 }
82 }